package org.despair2D.ui 
{
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.system.Capabilities;
	import flash.ui.Mouse;
	import flash.ui.Multitouch;
	import org.despair2D.ui.core.Component;
	import org.despair2D.Observer;
	import org.despair2D.core.ProcessManager
	import org.despair2D.debug.Logger;
	import org.despair2D.ui.core.ComponentProxy;
	import org.despair2D.ui.core.FusionComp;
	import org.despair2D.ui.core.IComponent;
	import org.despair2D.ui.core.IPanel;
	import org.despair2D.ui.core.UIManager;
	import org.despair2D.ui.puppet.NineScalePuppet;
	import org.despair2D.ui.puppet.supportClasses.ImagePuppetComp;
	import org.despair2D.ui.puppet.supportClasses.PuppetComp;
	import org.despair2D.ui.supportClasses.AbstractImageButton;
	import org.despair2D.ui.supportClasses.AbstractMovieClipButton;
	
	import org.despair2D.core.ns_despair;
	use namespace ns_despair;
	
	/**
	 * @facade
	 * @usage
	 * 
	 * [property]
	 * 			1. ◇pressedElement
	 * 			2. ◇hoveredElement
	 * 			3. ◇interactive
	 * 			4. ◇screenWidth
	 * 			5. ◇screenHeight
	 * 
	 * [method]
	 *			1. ◆startup
	 * 			2. ◆addImageButtonData
	 * 			3. ◆addMovieClipButtonData
	 * 			4. ◆addNineScaleData
	 * 			5. ◆registerPanel
	 * 			6. ◆getPanel
	 * 			7. ◆killAllPanels
	 * 			8. ◆addNullPressedListener
	 * 			9. ◆removeNullPressedListener
	 * 			10.◆stopAllDrag
	 */
final public class DespairUI
{
	
	/** ◇按下的组件 **/
	public static function get pressedElement() : IComponent { return UIManager.pressedElement }
	
	/** ◇触碰中的组件 **/
	public static function get hoveredElement() : IComponent { return UIManager.hoveredElement }
	
	/** ◇总交互开关 **/
	public static function get interactive() : Boolean { return UIManager.m_interactive }
	public static function set interactive( b:Boolean ) : void
	{
		UIManager.m_interactive = b 
		if (!b)
		{
			UIManager.reset()
		}
	}
	
	public static function get numComp() : Array { return [PuppetComp.cachedPuppetCompLength,
															ImagePuppetComp.cachedImageLength,
															FusionComp.cachedFusionCompLength] }
	
	/** ◇屏幕适配后的相对尺寸 **/
	public static function get screenWidth() : Number { return UIManager.m_screenWidth }
	public static function get screenHeight() : Number { return UIManager.m_screenHeight }
	
	
	/**
	 * ◆启动
	 * @param	invalidWhenLeave	滑出组件后是否失效
	 * @param	simulationWidth		模拟宽度
	 * @param	simulationHeight	模拟高度
	 * @param	landscape			landscape / portrait
	 */
	public static function startup( invalidWhenLeave:Boolean, simulationWidth:int, simulationHeight:int, landscape:Boolean = true ) : void
	{
		if (m_uiManager)
		{
			Logger.reportError('DespairUI', 'startup', 'despairUI has been started up !!')
			return
		}
		
		UIManager.m_invalidWhenLeave = invalidWhenLeave
		UIManager.m_stage = ComponentProxy.m_stage = ProcessManager.m_stage
		if (!ProcessManager.m_stage)
		{
			Logger.reportError('DespairUI', 'constructor', '主引擎(Despair)未启动 !!');
		}
		
		m_uiManager = new UIManager(simulationWidth, simulationHeight, landscape)
		
		trace('======================= [ Despair2D - mobileUI ] =======================');
		Logger.reportMessage('DespairUI', "★mobileUI引擎启动..." +
							"最大触摸点(" + Multitouch.maxTouchPoints + ")..." + 
							"设备dpi(" + Capabilities.screenDPI + ")..." + 
							"画像比例(" + UIManager.m_pixelRatio + ")..." + 
							"模拟尺寸(" + simulationWidth + ' | ' + simulationHeight + ")...\n")
		
		m_uiManager.initializeUI()
		m_uiManager.addMonitor()
	}
	
	/**
	 * ◆加入画像按钮数据
	 * @param	source
	 * @param	dataName
	 * @param	buttonType
	 */
	public static function addImageButtonData( source:*, dataName:String, buttonType:int ) : void
	{
		AbstractImageButton.addImageButtonData(source, dataName, buttonType)
	}
	
	/**
	 * ◆加入影片剪辑按钮数据
	 * @param	source
	 * @param	dataName
	 * @param	buttonType
	 */
	public static function addMovieClipButtonData( source:*, dataName:String, buttonType:int = 17 ) : void
	{
		AbstractMovieClipButton.addMovieClipButtonData(source, dataName, buttonType)
	}
	
	/**
	 * ◆加入九宫格画像数据
	 * @param	source			源画像
	 * @param	dataName		数据名称
	 * @param	scaleAreaSize	中心缩放区域尺寸
	 */
	public static function addNineScaleData( source:BitmapData, dataName:String, scaleAreaSize:int = 3 ) : void
	{
		NineScalePuppet.addNineScaleData(source, dataName, scaleAreaSize)
	}
	
	/**
	 * ◆注册面板
	 * @param	panelName
	 * @param	stateType
	 */
	public static function registerPanel( panelName:String, stateType:Class ) : void
	{
		m_uiManager.registerPanel(panelName, stateType)
	}
	
	/**
	 * ◆获取面板
	 * @param	panelName
	 */
	public static function getPanel( panelName:String ) : IPanel
	{
		return m_uiManager.getPanel(panelName)
	}
	
	/**
	 * ◆削除全部面板
	 */
	public static function killAllPanels() : void
	{
		m_uiManager.killAllPanels()
	}
	
	/**
	 * ◆加入空按下侦听器
	 * @param	listener
	 * @param	priority
	 */
	public static function addNullPressedListener( listener:Function, priority:int = 0 ) : void
	{
		(m_uiManager.m_nullPressedObserver ||= new Observer).addListener(listener, priority)
	}
	
	/**
	 * ◆移除空按下侦听器
	 * @param	listener
	 */
	public static function removeNullPressedListener( listener:Function ) : void
	{
		if (!m_uiManager.m_nullPressedObserver.removeListener(listener))
		{
			m_uiManager.m_nullPressedObserver = null
		}
	}
	
	/**
	 * ◆停止全部拖动
	 */
	public static function stopAllDrag( fireEvent:Boolean = false ) : void
	{
		ComponentProxy.stopAllDrag(fireEvent)
	}
	
	
	ns_despair static var m_uiManager:UIManager
}
}